package com.xujl.rxtask;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.xujl.task.RxExecutor;
import com.xujl.task.RxHelper;
import com.xujl.task.RxLife;
import com.xujl.task.RxLifeList;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class PerformanceActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "PerformanceActivity";
    private RxLifeList mLifeList = new RxLifeList();
    private int count;
    private List<Bitmap> mList = new ArrayList();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_performance);
        findViewById(R.id.btn1).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                test();
                break;
            default:

                break;

        }
    }

    private void test() {
        if (count > 1000) {
            return;
        }
        RxExecutor.getInstance()
                .executeTask(new RxHelper.DelayTask(1000) {
                    @Override
                    public void timeOver() {
                        Log.d(TAG, "任务完成");
                        count++;
                        for (int i = 0; i < 10; i++) {
                            mList.add(BitmapFactory.decodeResource(getResources(), R.drawable.test2));
                        }
                        test();
                    }

                    @Override
                    public boolean bindLife (RxLife rxLife) {
                        mLifeList.add(rxLife);
                        return super.bindLife(rxLife);
                    }

                });
    }

    @Override
    protected void onDestroy() {
        mLifeList.doUnbind();
        super.onDestroy();
    }
}
